package com.example.test;

import java.text.DecimalFormat;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

	protected static final int MENU_ABOUT = Menu.FIRST;
	protected static final int MENU_Quit = Menu.FIRST+1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViews();
		setListensers();
	}
	private Button button_calc;
	private EditText field_height;
	private EditText field_weight;
	private TextView view_result;
	private TextView view_suggest;
	private View popView=null;
	private PopupWindow popWin=null;
	private Button ok=null;
	
	private void findViews()
	{
		button_calc = (Button) findViewById(R.id.submit);
		field_height = (EditText) findViewById(R.id.height);
		field_weight = (EditText) findViewById(R.id.weight);
		view_result = (TextView) findViewById(R.id.result);
		view_suggest = (TextView) findViewById(R.id.suggest);
	}
	//Listen for button clicks
	private void setListensers() {
		button_calc.setOnClickListener(calcBMI);
	}
	private Button.OnClickListener calcBMI = new Button.OnClickListener()
	{
		public void onClick(View v)
		{
			DecimalFormat nf = new DecimalFormat("0.0");
			try{
				double height =	Double.parseDouble(field_height.getText().toString())/100;
				double weight =	Double.parseDouble(field_weight.getText().toString());
				double BMI = weight / (height * height);
				//Present result
				view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
				//Give health advice
				if(BMI>25){
					view_suggest.setText(R.string.advice_heavy);
				}else if(BMI<20){
					view_suggest.setText(R.string.advice_light);
				}else{
					view_suggest.setText(R.string.advice_average);
				}
			}
			catch(Exception err){
				Toast.makeText(MainActivity.this, R.string.input_error, Toast.LENGTH_SHORT).show();
			}
			/*
			LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
			MainActivity.this.popView=inflater.inflate(R.layout.popwindow, null);
			MainActivity.this.popWin=new PopupWindow(popView,500,420,true);
			MainActivity.this.ok= (Button) popView.findViewById(R.id.ok);
			MainActivity.this.ok.setOnClickListener(okWin);
			MainActivity.this.popWin.showAtLocation(MainActivity.this.button_calc, Gravity.CENTER, 0, 0);*/
	
			//openOptionsDialog();
		}
	};
	private Button.OnClickListener okWin = new Button.OnClickListener(){

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			MainActivity.this.popWin.dismiss();
		}
		
	};
	private void openOptionsDialog(){
		new AlertDialog.Builder(MainActivity.this)
			.setTitle(R.string.about_title)
			.setMessage(R.string.about_msg)
			.setPositiveButton(R.string.ok_label,new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			})
			.setNegativeButton(R.string.homepage_label, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int i) {
					// GOTO URL
					Uri uri=Uri.parse(getString(R.string.homepage_uri));
					Intent intent=new Intent(Intent.ACTION_VIEW,uri);
					startActivity(intent);
				}
			})
			.show();
//		Toast.makeText(MainActivity.this,R.string.about_title,Toast.LENGTH_SHORT).show();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		super.onCreateOptionsMenu(menu);
		menu.add(0,MENU_ABOUT,0,"关于..");
		menu.add(0,MENU_Quit,0,"结束");
		//getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	public boolean onOptionsItemSelected(MenuItem item){
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
			case MENU_ABOUT:
				openOptionsDialog();
				break;
			case MENU_Quit:
				finish();
				break;
		}
		return true;
	}

}
